from selenium import webdriver
from bs4 import BeautifulSoup
import time
import datetime
import pandas as pd

url = 'https://weibo.com/'
browser = webdriver.Chrome()
browser.maximize_window()
browser.get(url)
time.sleep(30)
url = 'https://s.weibo.com/top/summary?cate=realtimehot'

browser.get(url)
time.sleep(15)
html = browser.page_source
browser.quit()
soup = BeautifulSoup(html, 'lxml')
ranks = soup.select('td.td-01')
titles = soup.select('td.td-02 > a')
searches = soup.select('td.td-02 > span')
ranks = [i.get_text().strip() for i in ranks]
titles = [i.get_text().strip() for i in titles]
searches = [i.get_text().strip().split(' ')[-1] for i in searches]
searches = ['']+searches
all_news = {'序号': ranks, '关键词': titles,'热搜指数': searches}


df = pd.DataFrame(all_news)
today = datetime.date.today()
df.to_csv(f'新浪微博热搜榜-{today}.csv', index=False, encoding='utf-8-sig')

